package com.fernfx.xingtan.my.ui;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.TextureView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.BankInfoBean;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.verifycode.VerifyCodeSend;
import com.fernfx.xingtan.my.contract.MyBankCardsContract;
import com.fernfx.xingtan.my.presenter.MyBankCardsPresenter;
import com.fernfx.xingtan.utils.BankCardsUtils;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.RegexUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.fernfx.xingtan.view.textwatcher.AddSpaceTextWatcher;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imlib.RongIMClient;

/**
 * Created by admin on 2018/8/17.
 * 我的银行卡
 */

public class MyBankCardsActivity extends BaseActivity implements View.OnClickListener,MyBankCardsContract.View{
    @BindView(R.id.id_card_edit)
    EditText idCardEdit;
    @BindView(R.id.black_card_number_edit)
    EditText blackCardNumberEdit;
    @BindView(R.id.black_pnumber_edit)
    EditText blackPnumberEdit;
    @BindView(R.id.send_sms_code_tv)
    TextView sendSmsCodeTv;
    @BindView(R.id.input_sms_code_edit)
    EditText inputSmsCodeEdit;
    @BindView(R.id.name_edit)
    EditText nameEdit;
    @BindView(R.id.black_name_edit)
    EditText blackNameEdit;

    //presenter
    private MyBankCardsContract.Presenter presenter;
    private VerifyCodeSend verifyCodeSend = new VerifyCodeSend(this,"addBankCard");

    public static void start(Context context) {
        Intent intent = new Intent(context, MyBankCardsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_black_cards;
    }

    @Override
    protected void initListener() {
        setToolbarTitle("绑定银行卡");
        //身份证
        AddSpaceTextWatcher idCardTwatcher = new AddSpaceTextWatcher(idCardEdit,21);
        idCardTwatcher.setSpaceType(AddSpaceTextWatcher.SpaceType.IDCardNumberType);
        //银行卡号
        AddSpaceTextWatcher blackCardTwatcher = new AddSpaceTextWatcher(blackCardNumberEdit,48);
        blackCardTwatcher.setSpaceType(AddSpaceTextWatcher.SpaceType.bankCardNumberType);
        blackCardNumberEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String inputText = s.toString().trim();
                if (!TextUtils.isEmpty(inputText) && BankCardsUtils.checkBankCard(inputText)) {
                    BankInfoBean bankinfobean = new BankInfoBean(inputText);
                    blackNameEdit.setText(bankinfobean.getBankName());
                    //预留手机获得焦点
                    OtherUtil.editTextRequestFocus(blackPnumberEdit);
                }
            }
        });

        //手机号
        AddSpaceTextWatcher blackPnumberTwatcher = new AddSpaceTextWatcher(blackPnumberEdit,13);
        blackPnumberTwatcher.setSpaceType(AddSpaceTextWatcher.SpaceType.mobilePhoneNumberType);
    }

    @Override
    protected void initView() {
        presenter = new MyBankCardsPresenter();
        presenter.init(this);
    }

    @OnClick({R.id.my_backs_cards_clt,R.id.send_sms_code_tv,R.id.confirm_binding_bt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_backs_cards_clt:
                SoftInputUtil.hideKeyboard(this);
                break;
            case R.id.send_sms_code_tv:
                String pNumberText = trimEditText(blackPnumberEdit,true);
                if (TextUtils.isEmpty(pNumberText)) {
                    ToastUtil.showCentertoast("请输入手机号码");
                    return;
                }
                if (!RegexUtil.isMobileNumber(pNumberText)) {
                    ToastUtil.showCentertoast("手机号码不正确");
                    return;
                }
                //倒计时并发送验证码
                CountDownTimer countDownTimer = presenter.getTimeCount(sendSmsCodeTv);
                countDownTimer.start();
                //调用接口发送验证码
                verifyCodeSend.send(pNumberText);
                OtherUtil.editTextRequestFocus(inputSmsCodeEdit);
                break;
            case R.id.confirm_binding_bt:
                String nameText = trimEditText(nameEdit);
                if (TextUtils.isEmpty(nameText)) {
                    ToastUtil.showCentertoast("请输入姓名");
                    return;
                }
                String idCardText = trimEditText(idCardEdit,true);
                if (!RegexUtil.isCard(idCardText)) {
                    ToastUtil.showCentertoast("请输入正确的身份证号码");
                    return;
                }
                String blackNumberText = trimEditText(blackCardNumberEdit,true);
                if (!RegexUtil.isAccountNumber(blackNumberText)) {
                    ToastUtil.showCentertoast("请输入正确的银行卡号");
                    return;
                }
                String blackNameText = trimEditText(blackNameEdit);
                if (TextUtils.isEmpty(blackNameText)) {
                    ToastUtil.showCentertoast("请输入开户银行");
                    return;
                }
                String blackPnumberText = trimEditText(blackPnumberEdit,true);
                if (!RegexUtil.isMobileNumber(blackPnumberText)) {
                    ToastUtil.showCentertoast("请输入预留手机号");
                    return;
                }
                String smsCodeText = trimEditText(inputSmsCodeEdit);
                if (TextUtils.isEmpty(smsCodeText) || smsCodeText.length() != 6) {
                    ToastUtil.showCentertoast("请输入验证码");
                }
                //init request map
                requestArgsMap.clear();
                requestArgsMap.put("bankAccName",nameText);
                requestArgsMap.put("identityNum",idCardText);
                requestArgsMap.put("bankAccId",blackNumberText);
                requestArgsMap.put("bankDes",blackNameText);
                requestArgsMap.put("bankAccPhone",blackPnumberText);
                requestArgsMap.put("bankName",blackNameText);
                requestArgsMap.put("smsCode",smsCodeText);
                showLoading();
                presenter.request(requestArgsMap);
                break;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }
}
